﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibraryManager
{
    public partial class FindRecordForm : Form
    {
        private String _tag;
        public String _Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }
        private String _id;
        public String ID
        {
            get { return _id; }
            set { _id = value; }
        }
        private String _name;
        public String NAME
        {
            get { return _name; }
            set { _name = value; }
        }
        public FindRecordForm()
        {
            InitializeComponent();
        }

        private void FindRecord()
        {
            string sql = "select borrow.uId, users.uName, bNum, bName, bPrice, borrowDate, returnDate, Tag from borrow join users on borrow.uId = users.uId";
            MyDictionary dic = new MyDictionary();
            DataTable dt = SqlHelper.GetList(sql, dic);
            IddataGridView.DataSource = dt;
        }
        private void FindRecord(String type, String id)
        {
            string sql = "select borrow.uId, users.uName, bNum, bName, bPrice, borrowDate, returnDate, Tag from borrow join users on borrow.uId = users.uId";
            MyDictionary dic = new MyDictionary();
            try
            {
                if (type.Equals("bNum"))
                {
                    sql += " where bNum = '" + id + "'";
                    DataTable dt = SqlHelper.GetList(sql, dic);
                    IddataGridView.DataSource = dt;
                }
                else
                {
                    sql += " where borrow.uId = '" + id + "'";
                    DataTable dt = SqlHelper.GetList(sql, dic);
                    IddataGridView.DataSource = dt;
                }
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
        }

        private void FindRecord_Load(object sender, EventArgs e)
        {
            if(_Tag == "user")
            {
                adminPanel.Visible = false;
                string sql = "select ID,uId,bName,bNum,bPrice,borrowDate,returnDate,Tag from borrow where uId =@uId";
                MyDictionary dic = new MyDictionary();
                dic.Add("@uId", _id);
                DataTable dt = SqlHelper.GetList(sql, dic);
                dataGridView1.DataSource = dt;

                NametoolStripStatusLabel.Text = _name;
            }
            else
            {
                adminPanel.Visible = true;
                FindRecord();
            }
        }

        private void FindByUIdButton_Click(object sender, EventArgs e)
        {
            FindRecord("uId", IdTextBox.Text.Trim());
            IdTextBox.Text = "";
        }

        private void FindByBNumBtton_Click(object sender, EventArgs e)
        {
            FindRecord("bNum", bookNumTextBox.Text.Trim());
            bookNumTextBox.Text = "";
        }

        private void FindAllButton_Click(object sender, EventArgs e)
        {
            IdTextBox.Text = "";
            bookNumTextBox.Text = "";
            FindRecord();
        }
        #region 清除输入框
        private void IdTextBox_Enter(object sender, EventArgs e)
        {
            bookNumTextBox.Text = "";
        }

        private void bookNumTextBox_Enter(object sender, EventArgs e)
        {
            IdTextBox.Text = "";
        }
        #endregion
    }
}
